<?php

/*
 * 订单类
 */

namespace Home\Logic;

class OrderLogic {

    public function __construct() {
        $this->order = D('Action_order');
    }

    public function getActionOrderByStoreId($store_id,$start,$search,$barber_id,$status=-1,$length=12) {
        if($store_id == NULL || count($store_id)<1){
            return ;
        }
        $where = '';
        if($status>=0){
            $where[] = 'status='.$status;
        }
        if(is_mobile($search)){
            $where[] = "mobile='".$search."'";            
        }elseif($search != ''){
            $where[] = "order_number = '".$search."'";
        }
        if($barber_id != ''){
            $where[] = 'barber_id='.$barber_id;
        }
        $where[] = 'store_id in('.implode(',',(array)$store_id).")";
        return $this->order->where(implode(' and ',$where))->order('id desc')->limit($start,$length)->select();
    }
    
    public function getActionOrderByActionId($action_id,$start,$search,$barber_id,$status=-1,$length=12){
        if($status>=0){
            $arr['status'] = $status;
        }
        if(is_mobile($search)){
            $arr['mobile'] = $search;
        }elseif($search != ''){
            $arr['order_number'] = $search;
        }
        if($barber_id != ''){
            $arr['barber_id'] = $barber_id;
        }
        $arr['action_id'] = $action_id;
        return $this->order->where($arr)->order('id desc')->limit($start,$length)->select();
    }
    
    public function getActionOrderNumByActon($action_id){
        $where = "action_id=".$action_id." and status>=1";
        return $this->order->field('count(*) as num')->where($where)->find();
    }

    public function updateStatus($id,$type){
        return$this->order->where('id='.$id)->save($type);
    }
    
    public function createNumber(){  
        list($t1, $t2) = explode(' ', microtime());
        $num = (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
        return $num.rand(1000,9999);
    }

    public function getTeamNum($team_number){
        $o = D('Action_order');
        $where = 'team_number=\''.$team_number.'\' and status>=1';
        return $o->field('count(*) as num')->where($where)->find();
    }
    
    /*获取用户某个店铺的订单，区分店铺*/
    public function getUserOrder($store_id,$user_id,$start,$length=12){
        $o = D('Action_order');
        $where = 'store_id='.$store_id.' and client_user=\''.$user_id.'\' and status>0';
        return $o->where($where)->order('create_time desc')->limit($start,$length)->select();
    }
    
    /*获取用户所有订单，不区分店铺*/
    public function getUserOrderByUserId($user_id,$start,$length=12){
        $o = D('Action_order');
        $where = 'client_user=\''.$user_id.'\' and status>0';
        return $o->where($where)->order('create_time desc')->limit($start,$length)->select();
    }
    
    public function getUserOrders($storeids,$user_id,$start,$status=false,$length=12 ){
        if($storeids == NULL || count($storeids)<1){
            return ;
        }
        $o = D('Action_order');
        $storeids = implode(',', (array)$storeids);
        if($status===false){
            $where = 'store_id in('.$storeids.') and client_user=\''.$user_id.'\' and status>0 ';
        }elseif($status!=3){
            $where = 'store_id in('.$storeids.') and client_user=\''.$user_id.'\' and status='.$status;
        }elseif($status==3){ // status 3 暂时无用
            $where = 'store_id in('.$storeids.') and client_user='.$user_id;
        }
        return $o->where($where)->order('create_time desc')->limit($start,$length)->select();
    }

    public function getTeamLeader($team_number,$user_id){
        $o = D('Marketing_team');
        return $o->where(array('team_number'=>$team_number,'user_id'=>$user_id))->find();
    }
    
    public function getTeam($team_number,$limit=7){
        $o = D('Marketing_team');
        return $o->where(array('team_number'=>$team_number))->order('create_time desc')->limit($limit)->select();
    }
    
    public function getTeamCount($team_number){
        $o = D('Marketing_team');
        return $o->where(array('team_number'=>$team_number))->count();
    }
    
    public function getPayOrderCount($team_number){
        $user_id = $this->order->field('client_user')->where("team_number='{$team_number}' and status>0")->order('id desc')->select();
        $user = array();
        $o = D('Marketing_team');
        foreach((array)$user_id as $v){
            if($v['client_user']<1){
                continue;
            }
            $re = $o->where("team_number='{$team_number}' and user_id={$v['client_user']}")->find();
            $user[] = $re;
        }
        $count = count($user);
        if($count>0){
            return array('count'=>$count,'team'=>$user);
        }else{
            return false;
        }
    }
    
    public function getOrder($order_number){
        return $this->order->where(array('order_number'=>$order_number))->find();
    }
    
    public function updateOrder($order_no,$data){
        $re = $this->order->where(array('order_number'=>$order_no))->save($data);
        if($re!==false){
            return true;
        }
        return false;
    }
    
    /*支付专用*/
    public function _updateOrder($order_no,$data){
        $re = $this->order->where(array('order_number'=>$order_no))->save($data);
        return $re;
    }
    
    public function createTeam($data){
        $mt = D('Marketing_team');
        $data['create_time'] = time();
        return $mt->add($data);
    }

    // 创建订单 $order_info 为一个项目
    
    public function createOrder($data,$user_id,$team_user,$order_info,$return_option=false){
        $o = D('Action_order');
        $o->startTrans();
        $data['order_number'] = $this->createNumber();
        $data['create_time'] = time();
        $team_user['type'] = $team_user['team_number']==false?1:0;
        $team_user['team_number'] = $team_user['team_number']==false?$this->createNumber():$team_user['team_number'];
        $data['team_number'] = $team_user['team_number'];
        $team_user['user_id'] = $user_id;
        $id = $this->createTeam($team_user);
        if($id<1){
            $o->rollback();
            return false;
        }
        $id = $o->add($data);
        if($id<1){
            $o->rollback();
            return false;
        }
        if($order_info!=false && $order_info[0]['item_id']>0){
            $_order_info = new \Home\Logic\OrderinfoLogic();
                //此处添加 临时判断 判断是否有跨店商品
            
            
            
                //over
                foreach($order_info as $v){
                    $v['order_id'] = $id;
                    $v['order_number'] = $data['order_number'];
                    $v['create_time'] = $data['create_time'];
                    if(count($order_info)>1){
                        $v['type'] = $data['type'];
                    }else{
                        $v['type'] = 0;
                    }
                    $oinfo = $_order_info->insertOrderInfo($v);
                    if($oinfo<1){
                        $o->rollback();
                        return false;
                    }
                }
        }
        $o->commit();
        if($return_option == false){
            return $data['order_number'];
        }else{
            return $data['team_number'];
        }
    }

    // 创建订单 $order_info 为多个项目

    public function createOrder_multi( $data,$user_id,$team_user,$order_info,$return_option=false ){
        $o = D('Action_order');
        $o->startTrans();
        $data['order_number'] = $this->createNumber();
        $data['create_time'] = time();
        $team_user['type'] = $team_user['team_number']==false?1:0;
        $team_user['team_number'] = $team_user['team_number']==false?$this->createNumber():$team_user['team_number'];
        $data['team_number'] = $team_user['team_number'];
        $team_user['user_id'] = $user_id;
        $id = $this->createTeam($team_user);
        if($id<1){
            $o->rollback();
            return false;
        }
        $id = $o->add($data);
        if( $id<1 ){
            $o->rollback();
            return false;
        }
        if($order_info!=false && $order_info[0][0]['item_id']>0) {
            $type = explode( ',', $data['type'] );
            $_order_info = new \Home\Logic\OrderinfoLogic();

            for ($i = 0; $i < count($order_info); $i++) {
                foreach ($order_info[$i] as $v) {
                    $v['order_id'] = $id;
                    $v['order_number'] = $data['order_number'];
                    $v['create_time'] = $data['create_time'];
                    if ( count($order_info[$i]) > 1 ) {
                        $v['type'] = $type[$i];
                    } else {
                        $v['type'] = 0;
                    }
                    $oinfo = $_order_info->insertOrderInfo($v);
                    if ($oinfo < 1) {
                        $o->rollback();
                        return false;
                    }
                }
            }
        }
        $o->commit();
        if($return_option == false){
            return $data['order_number'];
        }else{
            return $data['team_number'];
        }

    }

    public function counts($store_id,$search,$barber_id,$status=-1){
        if($store_id == NULL || count($store_id)<1){
            return ;
        }
        $where = '';
        if($status>=0){
            $where[] = 'status='.$status;
        }
        if(is_mobile($search)){
            $where[] = "mobile='".$search."'";            
        }elseif($search != ''){
            $where[] = "order_number = '".$search."'";
        }
        if($barber_id != ''){
            $where[] = 'barber_id='.$barber_id;
        }
        $where[] = 'store_id in('.implode(',',(array)$store_id).")";
        return $this->order->where(implode(' and ',$where))->count();
    }
    
    public function countsByActionId($action_id,$search,$barber_id,$status=-1){
        if($action_id<1){
            return ;
        }
        $where[] = 'action_id='.$action_id;
        if($status>=0){
            $where[] = 'status='.$status;
        }
        if(is_mobile($search)){
            $where[] = "mobile='".$search."'";            
        }elseif($search != ''){
            $where[] = "order_number = '".$search."'";
        }
        if($barber_id != ''){
            $where[] = 'barber_id='.$barber_id;
        }
        return $this->order->where(implode(' and ',$where))->count();
    }
    
    public function checkOrder($action_id,$mobile){
        return $this->order->where('action_id='.$action_id.' and mobile=\''.$mobile.'\''.' and status>0')->find();
    }

    
    public function getOrderById($id){
        return $this->order->where('id='.$id)->find();
    }


    public function cartCreateOrder($data,$order_info,$return_option=false){
        $o = D('Action_order');
        $o->startTrans();
        $arr['mobile'] = $data['mobile'];
        $arr['price'] = $data['rmb'];
        $arr['store_id'] = $data['store_id'];
        $arr['client_user'] = $data['client_id'];
        $arr['order_number'] = $this->createNumber();
        $arr['create_time'] = time();
        $arr['app_id'] = 'nothing';
        $arr['type'] = $data['type'];
        $id = $o->add($arr);
        if($id<1){
            $o->rollback();
            return false;
        }
        if($order_info!=false && $order_info[0][0]['item_id']>0){
            $type = explode(',', $data['type']);
            $_order_info = new \Home\Logic\OrderinfoLogic();
            for($i=0;$i<count($order_info);$i++){
                foreach($order_info[$i] as $k=>$v){
                    $v['order_id'] = $id;
                    $v['order_number'] = $arr['order_number'];
                    $v['create_time'] = $arr['create_time'];
                    if(count($order_info[$i])>1){
                        $v['type'] = $type[$i];
                    }else{
                        $v['type'] = 0;
                    }
                    $oinfo = $_order_info->insertOrderInfo($v);
                    if($oinfo<1){
                        $o->rollback();
                        return false;
                    }
                }
            }
            
        }
        $o->commit();
        if($return_option == false){
            return $arr['order_number'];
        }
    }

    //发送短信用
    public function getCountByOrderNumber($order_number){
        $order = $this->order->where(array('order_number'=>$order_number))->where(' status>= 1 ')->find();
        if(!$order){
            return false;
        }else{
            $orders = $this->order->field('mobile,order_number')->where(array('team_number'=>$order['team_number']))->select();
             $count = count($orders);
            if($count>0){
                return array('count'=>$count,'mobile'=>$orders);
            }else{
                return false;
            }
        }
    }

    //拼团免费时,发送短信用
    public function getCountByTeamNumber($team_number){
        return $orders = $this->order->where(array('team_number'=>$team_number))->order('create_time desc')->select();
    }


    public function getPayOrderCountByAid($action_id){
        $user_id = $this->order->field('team_number,client_user')->where("action_id=$action_id and status>0")->order('id desc')->select();
        
        $user_id_= array();
        $client_user = array();
        $team_number_ = '';
        foreach($user_id as $v){
            if($team_number_ == $v['team_number']){
                continue;
            }
            $user_id_[]=$v['team_number']; 
            $client_user[] = $v['client_user'];
            $team_number_ = $v['team_number'];
        }
        if(count($client_user)<1){
            return false;
        }
        $user = array();
        $o = D('Marketing_team');
        $re = $o->where(array('team_number'=>array('IN',$user_id_),'user_id'=>array('IN',$client_user)))->order('id desc')->select();
        $count = count($re);
        $user_ = array_chunk($re,6);
        $user = $user_[0];        
        unset($user_);
        unset($re);
        if($count>0){
            return array('count'=>$count,'team'=>$user);
        }else{
            return false;
        }
    }

    //c端用
    public function countOrders($store_id,$client_id){
        $re = $this->order->where('client_user='.$client_id.' and store_id='.$store_id.' and status=4')->count();
        return $re;
    }

    
}
